<?php



namespace app\admin\controller;

use app\common\model\SmsLog;
use buildView\Grid;
use library\Controller;
use think\Db;

/**
 * 系统日志管理
 * Class Oplog
 * @package app\admin\controller
 */
class Oplog extends Controller
{

    /**
     * 指定当前数据表
     * @var string
     */
    public $table = 'SystemLog';

    /**
     * 系统操作日志
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $this->title = '系统操作日志';
        $query = $this->_query($this->table)->like('action,node,content,username,geoip');
        $query->dateBetween('create_at')->order('id desc')->page();
    }

    /**
     * 列表数据处理
     * @auth true
     * @param array $data
     * @throws \Exception
     */
    protected function _index_page_filter(&$data)
    {
        $ip = new \Ip2Region();
        foreach ($data as &$vo) {
            $result = $ip->btreeSearch($vo['geoip']);
            $vo['isp'] = isset($result['region']) ? $result['region'] : '';
            $vo['isp'] = str_replace(['内网IP', '0', '|'], '', $vo['isp']);
        }
    }

    /**
     * 清理系统日志
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function clear()
    {
        if (Db::name($this->table)->whereRaw('1=1')->delete() !== false) {
            $this->success('日志清理成功！');
        } else {
            $this->error('日志清理失败，请稍候再试！');
        }
    }

    /**
     * 删除系统日志
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }
    /**
     * 短信发送记录
     * @auth true
     * @menu true
     * @return string
     */
    public function sms()
    {

        $grid = new Grid(new SmsLog());
        $grid->setTitle('短信记录');
        $grid->result_type('发送结果类型')->using([
            0=>'失败',
            1=>'成功',
        ]);
        $grid->user()->nickname('会员')->hide();
        $grid->mid('会员')->display(function ($val, $data) {
            $img = "<img src='{$data['user']['headimg']}' data-tips-image='' height='80' width='80' style='border-radius: 50px'><br>";
            return $img . $data['user']['nickname'];
        });
        $grid->type_desc('作用类型');
        $grid->phone('目标手机');
        $grid->content('短信内容');
        $grid->result('返回结果');
        $grid->create_at('发送时间');
        $grid->filter(function ($filter) {
            $filter->like('user.nickname','会员');
            $filter->like('content','短信内容');
            $filter->like('result','返回结果');
            $filter->eq('phone','手机号码');
            $filter->eq('result_type','发送结果类型')->select([
                0=>'失败',
                1=>'成功',
            ]);
            $filter->dateBetween('create_at','发送时间');
        });
        $grid->hideAddButton();
        $grid->actions(function ($action) {
            $action->hideDetail();
            $action->hideEdit();
        });
        return $grid->view();
    }
}
